package com.apes.sms.systemSetting.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.sms.systemSetting.model.ServicePlanPrint;
import com.apes.sms.systemSetting.repository.ServicePlanPrintRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 功能：方案打印设置服务层
 *
 * @author: zyl
 * @create: 2022-07-01 14:10
 */
@Service("servicePlanPrintService")
public class ServicePlanPrintService extends DomainService {
    @Autowired
    private ServicePlanPrintRepository servicePlanPrintRepository;

    /**
     * 创建后保存触发的方法
     * 在菜单中 '创建' 配置对应的方法 save
     *
     * @param request
     * @return
     */
    public ServicePlanPrint save(SimpleRequest request) {
        ServicePlanPrint servicePlanPrint = request.getO(ServicePlanPrint.class);
        servicePlanPrint.setCreator(servicePlanPrint.getOperator());
        getLogoId(servicePlanPrint);
        check(servicePlanPrint);
        return servicePlanPrintRepository.saveAndFlush(servicePlanPrint);
    }

    private void getLogoId(ServicePlanPrint servicePlanPrint) {
        String jsonStr = servicePlanPrint.getLogo();

        if (!ObjectUtils.isEmpty(jsonStr)) {
            List logoList = JSONObject.parseArray(jsonStr);
            JSONObject jsonObject = (JSONObject) logoList.get(0);
            String logoId = jsonObject.getString("id");
            servicePlanPrint.setLogoId(logoId);
        }
    }

    private void check(ServicePlanPrint servicePlanPrint) {
        // 选择为公司
        if ("1".equals(servicePlanPrint.getRange())) {
            PartyRole company = servicePlanPrint.getCompany();
            Optional.ofNullable(company).orElseThrow( () -> new RuntimeException("公司信息为空") );
        }

        // 选择为门店
        if ("0".equals(servicePlanPrint.getRange())) {
            Dept dept = servicePlanPrint.getDept();
            Optional.ofNullable(dept).orElseThrow( () -> new RuntimeException("门店信息为空") );
        }
    }

    /**
     * 编辑后保存触发的方法
     * 在菜单中 '编辑' 配置为对应的方法 update
     * 
     * @param request
     * @return
     */
    public ServicePlanPrint update(SimpleRequest request) {
        String id = request.get("id");
        int version = request.get("version");

        // 从数据库中取数，检查数据
        ServicePlanPrint oldServicePlanPrint = servicePlanPrintRepository.findOne(id);
        if (oldServicePlanPrint == null) throw new RuntimeException("门店/公司【" + id + "】在系统中不存在。");
        if (oldServicePlanPrint.getVersion() != version) throw new RuntimeException("记录已更新或已删除，请重新提交！");

        ServicePlanPrint servicePlanPrint = request.getO(ServicePlanPrint.class);
        getLogoId(servicePlanPrint);

        //check(servicePlanPrint);
        return servicePlanPrintRepository.saveAndFlush(servicePlanPrint);
    }

    /**
     * 删除
     *
     * @param request
     * @return
     */
    public String delete(SimpleRequest request) {
        ServicePlanPrint servicePlanPrint = request.getO(ServicePlanPrint.class);
        servicePlanPrintRepository.delete(servicePlanPrint);
        return "删除成功";
    }

    public ServicePlanPrint findNowPrintSetting(SimpleRequest request){
        JSONObject jo = request.getJO();
        ServicePlanPrint result = null;

        ArrayList rows=new ArrayList();

        if (jo.get("dept") !=null){
            rows.add(MapUtil.mapper("field","dept.id","operator","EQ","value",jo.get("dept")));
        }
        if (jo.get("company") !=null){
            rows.add(MapUtil.mapper("field","company.id","operator","EQ","value",jo.get("company")));
        }
        result = servicePlanPrintRepository.findAll(JpaDsl.toCriteria(rows)).stream().findAny().orElse(null);

        if (jo.get("range") !=null){
            result = servicePlanPrintRepository.findOne("000000");
        }
        return result;
    }

}
